import type { Rule } from "antd/es/form"
import patterns from "./patterns"

interface  MessageParams {
    propertyName?: string,
    message?:string
}
// 必填项验证
export const requiredRule = ({ propertyName, message }: MessageParams ): Rule => {
    return {
        required: true,
            message: message ?? (propertyName ? `${propertyName}不能为空` : '此字段为必填项'),
    }
};
// 长度验证
export const lenRule = ({ propertyName, len, message }: MessageParams & { len: number } ): Rule => {
    return {
        len,
        message: (message ?? propertyName ? `${propertyName}` : "此字段") + `长度为${len}位`,
    }
};
// 最小值(长度)验证
export const minRule = ({ propertyName, fieldValueType, min, message }: MessageParams & {fieldValueType?: string, min: number} ): Rule => {
    return {
        min,
        message: (message ?? propertyName ? `${propertyName}` : "此字段") + (fieldValueType === "number" ? `不能小于${min}` : `长度不能低于${min}位`),
    }
};
// 最大值(长度)验证
export const maxRule = ({ propertyName, fieldValueType, max, message, }: MessageParams & {fieldValueType?: string, max: number} ): Rule => {
    return {
        max,
        message: (message ?? propertyName ? `${propertyName}` : "此字段") + (fieldValueType === "number" ? `不能大于${max}` : `长度不能超过${max}位`)
    }
}

/** 常用字段检验规则 **/
// 用户名
export const usernameRules:Rule[] = [
    requiredRule({propertyName: "用户名"}),
    { pattern: patterns.username, message: "用户名长度为6-20位，以字母开头，可包含字母、数字、下划线、点和连字符" }
]
// 密码
export const passwordRules:Rule[] = [
    requiredRule({propertyName: "密码"}),
    { pattern: patterns.password, message: "密码长度为8-20位，必须包含大小写字母、数字和特殊字符(@$!%*?&)" }
]
// 验证码
// 字符串验证码
export const codeRules:Rule[] = [
    requiredRule({propertyName: "验证码"}),
    lenRule({propertyName: "验证码", len: 4})
]
